package com.iflytek.universitymanagement.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iflytek.universitymanagement.config.Constants;
import com.iflytek.universitymanagement.config.RoleEnum;
import com.iflytek.universitymanagement.domain.*;
import com.iflytek.universitymanagement.exception.ServiceException;
import com.iflytek.universitymanagement.mapper.StudentMapper;
import com.iflytek.universitymanagement.mapper.StudentScoreMapper;
import com.iflytek.universitymanagement.service.StudentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author louyp
 * @since 2024-11-26
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Resource
    StudentMapper studentMapper;


    @Autowired
    private StudentScoreMapper studentScoreMapper;
    public void add(Student student) {
        // 判断一下该用户在数据库里有没有
        Student dbStudent = studentMapper.selectByName(student.getName());
        if (ObjectUtil.isNotEmpty(dbStudent)) {
            throw new ServiceException("账号已存在");
        }
        // 初始化一些页面上没有填写的信息
        if (ObjectUtil.isEmpty(student.getPassword())) {
            student.setPassword(Constants.USER_DEFAULT_PASSWORD);
        }
        if (ObjectUtil.isEmpty(student.getName())) {
            student.setName(student.getName());
        }
        student.setRole(RoleEnum.STUDENT.name());

        studentMapper.insertStudent(student);
    }


    @Override
    public Account login(Account account) {
        Account dbStudent = studentMapper.selectByName(account.getName());
        if (ObjectUtil.isNull(dbStudent)) {
            throw new ServiceException("账号不存在");
        }
        if (!account.getPassword().equals(dbStudent.getPassword())) {
            throw new ServiceException("账号或者密码错误");
        }
        // 生成token
        return dbStudent;
    }



    /**
     * 注册
     */
    public void register(Account account) {
        Student student = new Student();
        BeanUtils.copyProperties(account, student);
        add(student);
    }





    public boolean updatePassword(Long id, String newPassword) {
        Student student = studentMapper.selectById(id);
        if (student != null) {
            student.setPassword(newPassword);
            return studentMapper.updateById(student) > 0;
        }
        return false;
    }

//    @Override
//    public IPage<Student> selectPage(Student student, Integer pageNum, Integer pageSize) {
//        Page<Student> page = new Page<>(pageNum, pageSize);
//        return studentMapper.selectPage(page, null);
//    }


    // 根据学生姓名查询学生信息
    @Override
    public Student getStudentByName(String studentName) {
        return studentMapper.selectByName(studentName);  // 调用 Mapper 层的方法查询
    }

    @Override
    public void resetPassword(Student student) {
        Student dbStudent = studentMapper.selectByName(student.getName());
        if (dbStudent == null) {
            throw new ServiceException("用户不存在");
        }
        if (!student.getContact().equals(dbStudent.getContact())) {
            throw new ServiceException("验证错误");
        }
        dbStudent.setPassword("123456");//重置密码
        studentMapper.update(dbStudent);
    }

    // 根据学生名字获取学生ID
    public Integer getStudentIdByName(String name) {
        // 假设你的学生表中有 studentName 和 studentId 字段
        return studentMapper.getStudentIdByName(name);
    }

//    @Override
//    public List<StudentScore> getStudentScores(Integer studentId) {
//        return studentScoreMapper.getStudentScores(studentId);
//    }


}
